﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SLLottery.Common;

namespace SLLottery.Controls
{
    public partial class NumButton : UserControl
    {
        string ws = string.Empty;
        public NumButton()
        {
            InitializeComponent();
        }
        public string Text
        {
            set
            {
                this.tb.Text = value;
            }
            get
            {
                return this.tb.Text.ToString();
            }
        }
        public void SetClicked(bool isClicked)
        {
            Border border = (Border)tb.Parent;
            if (isClicked)
            {
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/Orange_Button.jpg", UriKind.Relative));
                border.Background = imageBrush;
                this.Tag = tb.Text;
                tb.Foreground = new SolidColorBrush(Colors.White);
            }
            else
            {
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/white_button.jpg", UriKind.Relative));
                border.Background = imageBrush;
                this.Tag = null;
                tb.Foreground = new SolidColorBrush(Colors.Black);
            }
        }
        public void Click(object sender)
        {
            TextBlock tb = (TextBlock)sender;
            Border border = (Border)tb.Parent;
            if (this.Tag != null)
            {
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/white_button.jpg", UriKind.Relative));
                border.Background = imageBrush;
                this.Tag = null;
                tb.Foreground = new SolidColorBrush(Colors.Black);
                if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                {
                    if (MainPage.dicWs.ContainsKey(ws)&&MainPage.dicWs[ws]==1)
                    {
                        MainPage.dicWs.Remove(ws);
                    }
                    else
                    {
                        MainPage.dicWs[ws] -= 1; ;
                    }
                }
            }
            else
            {
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/Orange_Button.jpg", UriKind.Relative));
                border.Background = imageBrush;
                this.Tag = tb.Text;
                tb.Foreground = new SolidColorBrush(Colors.White);
                if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
                {
                    if (MainPage.dicWs.ContainsKey(ws))
                    {
                        MainPage.dicWs[ws] += 1;
                    }
                    else
                    {
                        MainPage.dicWs.Add(ws,1);
                    }
                }
            }
        }

        private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ws = ((((this.Parent as StackPanel).Parent as StackPanel).Parent as StackPanel).Children[0] as BgButton).Text;
            if (MainPage.playTypeRadio == 47 || MainPage.playTypeRadio == 52)
            {
                int zdws = 0;
                if (MainPage.playTypeRadio == 47)
                {
                    zdws = 3;
                }
                else
                {
                    zdws = 2;
                }
                if (MainPage.dicWs.Count < zdws ||
                    MainPage.dicWs.ContainsKey(ws))
                {
                    Click(sender);
                    return;
                }
            }
            else
            {
                Click(sender);
            }
        }
    }
}
